import requests
from config.Xunengqi_qiping import xnqqp

xnq = xnqqp()


# 气瓶处理逻辑的主方法
def Xnq(url, device_name):
    try:
        # 发送GET请求
        response = requests.get(url)
        # 检查请求是否成功
        if response.status_code == 200:
            # 解析数据
            data = response.json()
            # 获取结果部分
            results = data.get("results", [])
            # 如果没有结果，直接返回空列表
            if not results:
                print(f"API {url} 返回结果为空")
                return []
            # 取最新的一条数据
            latest_result = results[-1]
            device_id = latest_result.get("id")
            device_data = latest_result.get("data", [])
            # 检查 device_data 是否是列表
            if not isinstance(device_data, list):
                print(f"设备 {device_name} 的数据格式错误，设备ID: {device_id}")
                return []
            # 输出设备的ID和数据
            print(f"设备 {device_name}的ID:{device_id}")
            print(f"设备 {device_name} 的数据: {', '.join(map(str, device_data))}")
            # 设置预设值和容差范围
            # 检查数据是否足够（每个设备有20个温度值）
            if len(device_data) < 20:
                print(f"设备 {device_name}的ID:{device_id} 热成像传感器损坏")
                return []
            # 将前10个温度值作为等高点温度，后10个温度值作为等中点温度
            high_points = device_data[:10]  # 前10个值为等高点温度
            mid_points = device_data[10:20]  # 后10个值为等中点温度

            # 计算等高点和等中点温度的平均值
            high_avg = sum(high_points) / len(high_points)
            mid_avg = sum(mid_points) / len(mid_points)
            gaowei=xnq.gaowei
            zhongwei=xnq.zhongwei
            # 设置一个开关，全部正常就输出正常
            all_normal = True
            # 遍历检查每个气瓶的温度
            for i in range(10):
                high_temp = high_points[i]
                mid_temp = mid_points[i]
                gao = high_temp - high_avg
                zhong=mid_temp-mid_avg
                if gao>gaowei or gao<-gaowei:
                    print(f"{device_name}的ID:{device_id} 等高点温度平均值异常，当前值: {high_temp},正常范围（+—2")
                elif zhong>zhongwei or zhong<-zhongwei:
                    print( f"设备 {device_name}的ID:{device_id} 等中点温度平均值异常，当前值: {mid_temp},正常范围（+—2")
                else:
                    print(f"设备 {device_name}的ID:{device_id} 所有气瓶检查完成！正常")
            return [latest_result]
        else:
            print(f"请求失败，状态码: {response.status_code}")
            return []
    except requests.RequestException as e:
        print(f"请求发生错误: {e}")
        return []


# 蓄能器安全阀1
def XnqX1():
    # 模拟从超声流量计获取流量数据
    url = "http://222.134.245.144:17000/chuanganqi/Csxnq01DeviceModel/"
    response = requests.get(url)
    if response.status_code == 200:
        data = response.json()
        results = data.get("results", [])
        latest_result = results[-1]
        xnqid = latest_result.get("id")
        xnqdata = latest_result.get("data")
        if xnqdata != 0:
            print(f"蓄能器1阀门泄露 瞬时流量为:{xnqdata}")
        else:
            print("蓄能器1阀门泄露未发现泄露")


# 蓄能器安全阀2
def XnqX2():
    # 模拟从超声流量计获取流量数据
    url = "http://222.134.245.144:17000/chuanganqi/Csxnq02DeviceModel/"
    response = requests.get(url)
    if response.status_code == 200:
        data = response.json()
        results = data.get("results", [])
        latest_result = results[-1]
        xnqid = latest_result.get("id")
        xnqdata = latest_result.get("data")
        if xnqdata != 0:
            print(f"蓄能器2阀门泄露 瞬时流量为:{xnqdata}")
        else:
            print("蓄能器2阀门泄露未发现泄露")


# 1号气瓶
def Qip1():
    url1 = "http://222.134.245.144:17000/chuanganqi/Rcx01DeviceModel/"
    url2 = "http://222.134.245.144:17000/chuanganqi/Rcx02DeviceModel/"
    Xnq(url1, "1号1-10设备")
    Xnq(url2, "1号11-20设备")
    print(f"1号设备检查完成")


# 2号气瓶
def Qip2():
    url1 = "http://222.134.245.144:17000/chuanganqi/Rcx03DeviceModel/"
    url2 = "http://222.134.245.144:17000/chuanganqi/Rcx04DeviceModel/"
    Xnq(url1, "2号1-10设备")
    Xnq(url2, "2号11-20设备")
    print(f"2号设备检查完成")
Qip1()
Qip2()

